
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Tathata.Common.BLL.Entities;
using Tathata.Common.BLL.Managers;
using Tathata.Common.Utils;
using Tathata.Common.BLL.EntityCollections;

namespace Tathata.Web.Administration
{
    public partial class Administration_ProductReviews : BaseNopAdministrationPage
    {
        protected override void OnInit(EventArgs e)
        {
            InitializeComponent();
            base.OnInit(e);
        }

        private void InitializeComponent()
        {
            this.dlProductReviews.UpdateCommand += new DataListCommandEventHandler(dlProductReviews_Update);
            this.dlProductReviews.EditCommand += new DataListCommandEventHandler(dlProductReviews_Edit);
            this.dlProductReviews.DeleteCommand += new DataListCommandEventHandler(dlProductReviews_Delete);
        }

        private void dlProductReviews_Update(object sender, DataListCommandEventArgs e)
        {
            int productReviewID = int.Parse(((DataList)sender).DataKeys[e.Item.ItemIndex].ToString());
            ProductReview productReview =ProductReviewManager.GetByProductReviewID(productReviewID);
            if (productReview != null)
            {
                ProductReviewManager.UpdateProductReview(productReview.ProductReviewID,
                    productReview.ProductID, productReview.CustomerID, productReview.Title,
                    productReview.ReviewText, productReview.Rating, productReview.HelpfulYesTotal, productReview.HelpfulNoTotal, !productReview.IsApproved, productReview.CreatedOn);
            }
            BindData();
        }

        private void dlProductReviews_Edit(object sender, DataListCommandEventArgs e)
        {
            int productReviewID = int.Parse(((DataList)sender).DataKeys[e.Item.ItemIndex].ToString());
            Response.Redirect("ProductReviewDetails.aspx?ProductReviewID=" + productReviewID.ToString());
            
        }

        private void dlProductReviews_Delete(object sender, DataListCommandEventArgs e)
        {
            int productReviewID = int.Parse(((DataList)sender).DataKeys[e.Item.ItemIndex].ToString());
            ProductReviewManager.DeleteProductReview(productReviewID);
            BindData();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
                BindData();
        }

        protected string GetCustomerInfo(int CustomerID)
        {
            Customer customer = CustomerManager.GetByCustomerID(CustomerID);
            if (customer != null)
            {
                string customerInfo = string.Format("<a href=\"CustomerDetails.aspx?CustomerID={0}\">{1}</a>", customer.CustomerID, Server.HtmlEncode(customer.Email));
                return customerInfo;
            }
            else
                return string.Empty;
        }

        protected void BindData()
        {
            ProductReviewCollection productReviewCollection = null;
            if (this.ProductID > 0)
                productReviewCollection = ProductReviewManager.GetByProductID(ProductID);
            else
                productReviewCollection = ProductReviewManager.GetAllProductReviews();
            dlProductReviews.DataSource = productReviewCollection;
            dlProductReviews.DataBind();
        }
        
        public int ProductID
        {
            get
            {
                return CommonHelper.QueryStringInt("ProductID");
            }
        }
    }
}
